<?php
/*
   Copyright 2012 BiSe Trojanov

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
 require_once('config.php');

?><!doctype html><html>
<head>
 <?php include('./include/header.inc'); ?>
 <title><?php echo $LANGS['pictures']['title'].' / '.$sad_domain; ?></title>
 <script src="<?php echo $sad_baseurl; ?>/common/bquery_ajax.js"></script>
 <script src="./admin.js.php"></script>
</head>
<body>
<?php include('./include/toolbar.inc');
 if (!function_exists('ImageResize')){echo 'Function ImageResize not found</body></html>';exit;}
 if (!function_exists('imagecreatetruecolor')){echo 'Module GD not found</body></html>';exit;}
 
 echo '<div class="infobox">'.$LANGS['pictures']['infobox'].'</div>';
 
 $t1=explode('/',$_POST['destdir']);$t2=$sad_root.'/storage/';
 foreach ($t1 as $t3){
  if ($t3=='.' or $t3=='..' or $t3==''){continue;}
  $t2.=$t3.'/';
  if (!file_exists($t2)){mkdir($t2);echo 'Folder <b>'.$t3.'</b> created<br/>';}
 }

 foreach($_POST as $key => $value){
  if (!preg_match('|^file_[0-9]+$|',$key)){continue;}

  $old =$sad_root.'/storage/'.sad_safe_path($_POST['sourcedir']).'/'.$value;
  $md5 =md5_file($old);$ext=preg_replace('|^.*\\.([a-z]+)$|','$1',$value);
  $new =$t2.$md5.'.'.$ext;
  $new2=$t2.'.thumbs/1_'.$md5.'.'.$ext;

  if ($old!==$new){copy($old,$new);}
  $t=ImageResize($new,$new2,$_POST['width'],$_POST['height']);
  echo '<br /><a href="'.$sad_baseurl.'/storage/'.$_POST['destdir'].'/.thumbs/1_'.
       $md5.'.'.$ext.'" target="_blank">'.$new2.'</a> <b>'.filesize($new2).
       '</b> bytes';
 }
 unset($t,$t1,$t2,$t3);

?>

<a href="javascript:" onclick="set_mode('a');" class="func">Access time</a>, 
<a href="javascript:" onclick="set_mode('m');" class="func">Change time</a>, 
<a href="javascript:" onclick="set_mode('c');" class="func">Inode time</a><br><br>
 
<!-- new code -->
<form method="post">
<!-- Source -->
<input type="hidden" name="sourcedir" value="/" id="path0">
<div class="browser" id="browser0"></div>
<script>
function check(){
 var k=1;while (null!==(d=document.getElementById('file_'+k))){
  d.checked=true;k++;
 }
}

function uncheck(){
 var k=1;while (null!==(d=document.getElementById('file_'+k))){
  d.checked=false;k++;
 }
}

function revert(){
 var k=1; while (null!==(d=document.getElementById('file_'+k))){
  d.checked=!d.checked;k++;
 }
}

</script>
<a href="javascript:" onclick="check();"><img src="./style/ui-check-box.png" title="Check all"></a>
<a href="javascript:" onclick="uncheck();"><img src="./style/ui-check-box-uncheck.png" title="Uncheck all"></a>
<a href="javascript:" onclick="revert();"><img src="./style/ui-check-boxes.png" title="Revert all"></a><br />

<!-- Destination -->
Destination: <input name="destdir" id="path1" value="/" style="width: 250px;"><br />
<div class="browser" id="browser1"></div>
<input id="new_folder_name"><br><input type="button" class="nicebutton" value="Create folder" onclick="CreateFolder('storage', 0, 1);"><br/><br/>
<script>
 var expl_set     =[
  {base_path:'/', parent_folder: 'storage', g_type: 1, need_check: true},
  {base_path:'/', parent_folder: 'storage', g_type: 0, need_check: false}
 ];
 viewfolder('/', 0);
 function aff(){
  if (spider_locked){setTimeout('aff()',100);return;}
  viewfolder('/', 1);
 }
 setTimeout('aff()',100);
</script>


<input name="width" value="300"> Width<br />
<input name="height" value="auto"> Height<br />

<input type="submit" value="Copy images" class="nicebutton">
<input type="hidden" name="action" value="upload">
</form>


</body></html>